﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kristall.Managers;
using Kristall.Screens;

namespace Kristall.Screens
{
    public class PauseMenuScreen : MenuScreen
    {
        GameplayScreen gameplayScreen;

        public PauseMenuScreen(GameplayScreen gameplayScreen) : base("Game Paused")
        {
            IsPopup = true;
            this.gameplayScreen = gameplayScreen;

            MenuEntry continueMenuEntry = new MenuEntry("Continue");
            continueMenuEntry.OnSelect += new EventHandler(OnCancel);

            MenuEntry exitMenuEntry = new MenuEntry("Exit");
            exitMenuEntry.OnSelect += new EventHandler(exitMenuEntry_OnSelect);

            MenuEntries.Add(continueMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        void exitMenuEntry_OnSelect(object sender, EventArgs e)
        {
            MessageScreen msg;

            if (gameplayScreen.LaunchedFromEditor)
            {
                msg = new MessageScreen("Are you sure you want to exit the game\n and go back to the level editor?", Buttons.YesNo);
            }
            else
            {
                msg = new MessageScreen("Are you sure you want to exit the game\n and go back to the main menu?", Buttons.YesNo);
            }
            
            msg.OnYes += new EventHandler(msg_OnYes);
            ScreenManager.AddScreen(msg);
        }

        LevelEditor levelEditor;
        LoadingScreen loadingScreen;

        void msg_OnYes(object sender, EventArgs e)
        {
            gameplayScreen.ExitScreen();
            if (gameplayScreen.LaunchedFromEditor)
            {
                levelEditor = new LevelEditor(ScreenManager.Game);
                loadingScreen = new LoadingScreen(levelEditor);
                loadingScreen.OnLoadingCompleted += new EventHandler(loadingScreen_OnLoadingCompleted);
                ScreenManager.AddScreen(loadingScreen);
            }
            else
            {
                ScreenManager.AddScreen(new BackgroundScreen());
                ScreenManager.AddScreen(new MainMenuScreen());
            }
        }

        void loadingScreen_OnLoadingCompleted(object sender, EventArgs e)
        {
            levelEditor.ToolPalette.Show();
        }
    }
}
